package mobilesmil.activity.composer;

import mobilesmil.activity.R;
import mobilesmil.data.SmilDataSet;
import mobilesmil.data.SmilResource;
import mobilesmil.data.SmilTextResource;
import mobilesmil.userinterface.BackgroundColorBox;
import mobilesmil.userinterface.ColorPickerDialog;
import mobilesmil.userinterface.ColorPickerDialog.OnColorChangedListener;
import mobilesmil.utils.ColorUtils;
import mobilesmil.utils.FormValidationUtils;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
/**
 * Provides a simple form for editing the background color and file name of a {@link SmilDataSet}.
 * The background color is changed via the {@link ColorPickerDialog}.
 * @author Ryan Ware
 *
 */
public class GeneralPropsFormActivity extends Activity {
	private EditText mEditFileName;
	private Button mBtnDone;
	private Button mBtnBackgroundColor;
	private ImageView mImageBackgroundColor;
	private int mChosenBackgroundColor;
	
	private static final int DIALOG_COLOR_PICKER_ID = 0;
	private static final int DIALOG_FORM_INVALID_ID = 1;
	
	private static String mDialogInvalidFormTitle = "";
	
	SmilDataSet editingDataSet = ComposerActivity.getEditingDataSet();
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.properties_form_general);
        
        mEditFileName = (EditText)findViewById(R.id.editFileName);
        mBtnBackgroundColor = (Button)findViewById(R.id.btnBackgroundColor);
        mBtnDone = (Button)findViewById(R.id.btnDone);
        mImageBackgroundColor = (ImageView)findViewById(R.id.imageBackgroundColor);
        mChosenBackgroundColor = editingDataSet.getBackgroundColor().getColor();
        
        mEditFileName.setText(editingDataSet.getFileName());
        mImageBackgroundColor.setBackgroundDrawable(new BackgroundColorBox(editingDataSet.getBackgroundColor().getColor()));
        mBtnBackgroundColor.setOnClickListener(new View.OnClickListener(){
        	public void onClick(View view) {
        		showDialog(DIALOG_COLOR_PICKER_ID);
        	}
        });
        
        mBtnDone.setOnClickListener(new View.OnClickListener(){
        	public void onClick(View view) {
        		if(fieldsAreValid()) 
        		{
        			editingDataSet.setFileName(mEditFileName.getText().toString());
            		editingDataSet.setBackgroundColor(ColorUtils.getHexStringForARGB(mChosenBackgroundColor));
            		updateAnyTextResources();
            		setResult(RESULT_OK);
            		finish();
        		}
        		else {
        			mDialogInvalidFormTitle = "Please enter a valid file name.";
        			showDialog(DIALOG_FORM_INVALID_ID);
        		}
        	}
        });
	}
	
	/**
	 * Updates the text color of any {@link SmilTextResource}s that exist in the editing {@link SmilDataSet} so
	 * they are the opposite of the chosen background color.
	 */
	private void updateAnyTextResources(){
		for(int i = 0; i < editingDataSet.getResourcesByBeginTime().size(); i ++)
		{
			if(editingDataSet.getResourcesByBeginTime().get(i).getType() == SmilResource.TYPE_TEXT)
			{
				editingDataSet.getResourcesByBeginTime().get(i).getSmilRegion().setColor(editingDataSet.getBackgroundColor());
				((SmilTextResource)editingDataSet.getResourcesByBeginTime().get(i)).updateText();
			}
		}
	}
	
	/**
	 * Ensures user input fields contain valid values.
	 * @return true if all user input fields are valid, false if any are not
	 */
	private boolean fieldsAreValid() {
		String value = mEditFileName.getText().toString();
		
		if(value.length() == 0)
			return false;
		
		for(char invalidChar : FormValidationUtils.INVALID_FILENAME_CHARS)
		{
			if(value.indexOf(invalidChar) >= 0) // value contains invalidChar
				return false;
		}
		
		return true;
	}
	
	private ColorPickerDialog.OnColorChangedListener colorChangeListener = new OnColorChangedListener() {
		@Override
		public void colorChanged(int color) {
			mChosenBackgroundColor = color; // update the chosen background color from the ColorPickerDialog
		}
	};
	
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		if(hasFocus) { // draws the chosen background color when the ColorPickerDialog is closed
			mImageBackgroundColor.setBackgroundDrawable(new BackgroundColorBox(mChosenBackgroundColor));
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		super.onCreateDialog(id);
		Dialog dialog = null;
		switch(id)
		{
			case DIALOG_COLOR_PICKER_ID:
				dialog = new ColorPickerDialog(this, colorChangeListener, mChosenBackgroundColor);
				break;
			case DIALOG_FORM_INVALID_ID:
		        dialog = FormValidationUtils.createInvalidFormDialog(this);
		        dialog.setTitle(mDialogInvalidFormTitle);
				break;
		}
		return dialog;
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		super.onPrepareDialog(id, dialog);
		switch(id)
		{
			case DIALOG_COLOR_PICKER_ID:
				dialog = new ColorPickerDialog(this, colorChangeListener, mChosenBackgroundColor);
				break;
		}
	}

	@Override
	public void onBackPressed() {
		if(ComposerActivity.isCreatingNewSmil()) { // if creating a new SMIL, return to home screen
			setResult(ComposerActivity.RESULT_CODE_NEW_BACK_BUTTON);
		}
		finish();
	}

}
